package com.rf.richfitwheel.admin.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.feignclient.FWfTaskService;
import com.rf.richfitwheel.admin.sys.dao.VacationMapper;
import com.rf.richfitwheel.admin.sys.model.Vacation;
import com.rf.richfitwheel.admin.sys.model.VacationExample;
import com.rf.richfitwheel.admin.sys.service.VacationService;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.UuidUtil;

import com.rf.richfitwheel.workflow.conf.WorkflowContants;
import com.rf.richfitwheel.workflow.entity.WfTaskParam;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DemoServiceImpl
 * @Author jiaozhongcai
 * @Date 2021/5/25 10:42
 * @Description TODO
 **/
@Service
public class VacationServiceImpl implements VacationService {

    @Autowired
    private VacationMapper vacationMapper;
    @Autowired
    private FWfTaskService fWfTaskService;

    @Override
    public PageUtils list(Map<String, Object> params, UserVO user) {

        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Vacation> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        String jsonStr = JSONObject.fromObject(params.get("dataForm")).toString();
        VacationExample example = new VacationExample();
        VacationExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        example.setOrderByClause(" apply_date desc ");
        List<Vacation> wfModelExtends = vacationMapper.selectByExample(example);

        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public Vacation getVacationById(String id) {
        return vacationMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addVacation(Vacation vacation, UserVO user) {
        Date nowDate = new Date();
        vacation.setId(UuidUtil.get32UUID());
        vacation.setApplyDate(nowDate);
        vacation.setUserId(user.getId());
        vacation.setStatus(WorkflowContants.PROCESS_STATUS_0);//暂存
        vacationMapper.insert(vacation);
    }

    @Override
    public void updateVacation(Vacation vacation, UserVO user) {
        vacation.setUserId(user.getId());
        vacationMapper.updateByPrimaryKeySelective(vacation);
    }

    @Override
    public void addAndSubmitVacation(Map<String, Object> params, UserVO user) {
        Vacation vacation = (Vacation) JSONObject.toBean(JSONObject.fromObject(params.get("vacation")), Vacation.class);
        WfTaskParam wfTaskParam = (WfTaskParam) JSONObject.toBean(JSONObject.fromObject(params.get("wfTaskParam")), WfTaskParam.class);
        if (StringUtils.isBlank(vacation.getId())) {
            this.addVacation(vacation, user);
        }
        wfTaskParam.setUserVO(user);
        wfTaskParam.setBusinessKey(vacation.getId());//业务主键
        wfTaskParam.setApproveNo(vacation.getApproveNo());//审批编号
        Map<String, Object> variables = wfTaskParam.getBaseParam(user);//组装流程参数
        variables.put("workDays", vacation.getWorkDays());
        wfTaskParam.setVariables(variables);
        R r = fWfTaskService.submitTask(wfTaskParam);
        wfTaskParam = (WfTaskParam) r.get("wfTaskParam");//
        vacation.setStatus(WorkflowContants.PROCESS_STATUS_1);//审批中
        vacation.setProcessInstanceId(wfTaskParam.getProcessInstanceId());
        this.updateVacation(vacation, user);
    }

    @Override
    public void updateAndSubmitVacation(Map<String, Object> params, UserVO user) {
        Vacation vacation = (Vacation) JSONObject.toBean(JSONObject.fromObject(params.get("vacation")), Vacation.class);
        WfTaskParam wfTaskParam = (WfTaskParam) JSONObject.toBean(JSONObject.fromObject(params.get("wfTaskParam")), WfTaskParam.class);
        Map<String, Object> variables = wfTaskParam.getVariables();//组装流程参数
        wfTaskParam.setVariables(variables);
        wfTaskParam.setUserVO(user);
        //wfTaskParam.setSubmitToPrev("y");//是否提交到上次退回的节点
//        wfTaskParam = fWfTaskService.submitTask(wfTaskParam);
        R r = fWfTaskService.submitTask(wfTaskParam);
        wfTaskParam = (WfTaskParam) r.get("wfTaskParam");//

    }

    @Override
    public void updateVacation(Vacation Vacation) {
        vacationMapper.updateByPrimaryKeySelective(Vacation);
    }

    @Override
    public void temp(Map<String, Object> params, UserVO user) {
        Vacation Vacation = (Vacation) JSONObject.toBean(JSONObject.fromObject(params.get("vacation")), Vacation.class);
        this.addVacation(Vacation, user);
    }
}
